#ifndef VNavItemTable_H
#define VNavItemTable_H					 

#include "VAction.h"
#include "VNavItemTableInfo.h"
#include "VDialog.h"
#include "VSqlTable.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VNavFieldPropertyEditor : public QVBoxLayout
{
	Q_OBJECT

public:
	static VSqlFieldPtr factoryFieldDialog(const VSqlTable &sqlTable,QWidget *parent,QAction *action = NULL);
		
public:
	VNavFieldPropertyEditor(QWidget *parent = NULL);

	VSqlFieldPtr field() const;

private slots:
	void typeChanged(int &v);

private:
	VPropEditor *newPropEditor();

private:	
	VSqlFieldPtr m_field;
	VPropEnum *m_propType;
	VPropEditor *m_typeEditor;
	VPropEditor *m_propEditor;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VNavItemTableSettings : public QSettings
{
public:
	VNavItemTableSettings(const QString& groupName);
	~VNavItemTableSettings() { endGroup();}

private:
	struct VInit
	{
		VInit();

		QString m_iniFile;
	};
	static VInit& init() { static VInit i; return i;}
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VNavItemViewTableRecords : public VNavItemView
{
	Q_OBJECT

public:
	enum EAction
	{
		EAction_tableInfo,
		EAction_records,
		EAction_addField,
	};
	typedef QHash<EAction,QAction*> VActions;

public:
	VNavItemViewTableRecords(QWidget *parent = NULL);
	~VNavItemViewTableRecords();
	
	virtual VNavItemType::EType eType() const { return VNavItemType::EType_tableRecords;}
	
	VActions actions() const { return m_actions;}

	virtual void setItemModel(VNavItemModel *itemModel)	{}
	virtual void setItem(VNavItem *item,const QModelIndex &index);

	//virtual void addToolBars(QList<QToolBar*> &toolBars) { toolBars << m_toolBar;}

public slots:
	void updateActions();
	void execAction(QAction *action);

protected: // VNavItemView
	void focusInEvent(QFocusEvent *event);
	void focusOutEvent(QFocusEvent *event);

private slots:
	void slot_horzSectionClicked(int logicalIndex);

private:
	void addAction(EAction eAction,VAction *action);
	
	void saveSettings();
	void restoreSettings();

	VSqlTable m_sqlTable;
	VActions m_actions;
	//QToolBar *m_toolBar;
	QLabel *m_info;
	VSqlTableModel *m_tableModel;
	QTableView *m_tableView;
};

///////////////////////////////////////////////////////////////////////////////

class VNavItemTableRecords : public VNavItem
{
	Q_OBJECT
	
public:
	VNavItemTableRecords();
	~VNavItemTableRecords();

	void setTableName(const QString &v) { m_tableName = v;}
	QString tableName() const { return m_tableName;}

	virtual VNavItemType::EType eType() const { return VNavItemType::EType_tableRecords;}
	virtual QString displayName() const { return m_tableName;}

	virtual VNavItemViewPtr factoryItemView(QWidget *parent) const { return VNavItemViewPtr(new VNavItemViewTableRecords(parent));}

	virtual bool canFetchMore() const { return childCount() == 0;}
	virtual QList<VNavItem*> fetchMore();
	
private:
	QString m_tableName;
};

///////////////////////////////////////////////////////////////////////////////

#endif
